package com.yangsir.mysite.controller;

import com.yangsir.mysite.common.R;
import com.yangsir.mysite.pojo.User;
import com.yangsir.mysite.service.UserService;
import com.yangsir.mysite.utils.AlimsgTemple;
import com.yangsir.mysite.utils.RedisUtils;
import com.yangsir.mysite.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * java 8
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private AlimsgTemple alimsgTemple;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 登录
     * @param useraccount
     * @param password
     * @return
     */
    @GetMapping("/login")
    public R<User> login(HttpServletRequest httpServletRequest,  String useraccount, String password) {
        if (useraccount==null || password==null) {
            return R.error("用户名或密码不能为空");
        }
        return userService.login(httpServletRequest, useraccount, password);
    }

    /**
     * 注册
     * @param
     * @return
     */
    @PostMapping("/register")
    public R<String> register(@RequestBody Map map) {
        if (map.size() != 4) {
            return R.error("注册信息缺失");
        }
        User user = new User();
        user.setUserAccount(map.get("userAccount").toString());
        user.setUserPassword(map.get("userPassword").toString());
        user.setPhone(map.get("phone").toString());
        String phoneCode = map.get("phoneCode").toString();
        String code = (String) redisUtils.get(map.get("phone").toString());
        if (phoneCode.equals("") || code == null || !phoneCode.equals(code)) {
            return R.error("验证码错误");
        }
        return userService.register(user);
    }

    @GetMapping("/sendPhoneCode")
    public R<String> createPhoneCode(String phone) {
        if (phone == null) {
            return R.error("手机号不能为空");
        }
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        alimsgTemple.sendMsg(phone, code);
        redisUtils.Tset(phone, code, 5);
        return R.success("发送成功");
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/logout")
    public R<String> Logout(HttpServletRequest httpServletRequest) {
        httpServletRequest.getSession().setAttribute("user", null);
        return R.success("退出成功");
    }

    /**
     * 管理员校验
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/checkManager")
    public R<String> checkManager(HttpServletRequest httpServletRequest) {
        User user = (User) httpServletRequest.getSession().getAttribute("user");
        if (user == null) {
            return R.error("身份过期,请重新登陆");
        }
        if (user.getUserRole() == 1 || user.getUserRole() == 2) {
            return R.success("校验通过");
        }
        return R.error("权限不足");
    }

    /**
     * 更新用户自己的信息 管理员更新
     * @param user
     * @return
     */
    @PostMapping("/updateUser")
    public R<User> updateUser(HttpServletRequest httpServletRequest, @RequestBody User user) {
        if (user==null) {
            return R.error("用户信息缺失");
        }
        return userService.upadteUser(httpServletRequest, user);
    }

    /**
     * 获取一位用户信息
     * @param id
     * @return
     */
    @GetMapping("/getUser")
    public R<User> getUser(Long id) {
        User byId = userService.getById(id);
        if (byId == null) {
            return R.error("用户不存在");
        }
        byId.setUserPassword(null);
        return R.success(byId, "获取成功");
    }

    /**
     * 获取所有用户信息 (管理员)
     * @return
     */
    @GetMapping("/getAllUser")
    public R<List<User>> getAllUser() {
        return userService.getAllUser();
    }

    /**
     * 创建用户 (管理员)
     * @param user
     * @return
     */
    @PostMapping("/createUser")
    public R<String> createUser(@RequestBody User user) {
        if (user==null) {
            return R.error("用户信息不能为空");
        }
        return userService.createUser(user);
    }

    /**
     * 搜索用户信息 (管理员)
     * @param str
     * @return
     */
    @GetMapping("/getUserByStr")
    public R<List<User>> selectUserByStr(String str) {
        if (str == null) {
            return R.error("查询信息不能为空");
        }
        return userService.selectUserByStr(str);
    }

    /**
     * 根据角色查询用户信息 (管理员)
     * @param userRole
     * @return
     */
    @GetMapping("/getUserByRole")
    public R<List<User>> getUserByRole(Integer userRole) {
        if (userRole == null) {
            return R.error("查询信息不能为空");
        }
        return userService.getUserByRole(userRole);
    }

    /**
     * 删除用户信息 (管理员)
     * @param id
     * @return
     */
    @DeleteMapping("/deleteUser")
    public R<String> deleteUserById(Long id) {
        if (id == null) {
            return R.error("查询信息不能为空");
        }
        return userService.deleteUserById(id);
    }

    /**
     * 禁用或启动用户 (管理员)
     * @param user
     * @return
     */
    @PostMapping("/deactivateUser")
    public R<String> deactivateUser(@RequestBody User user) {
        if (user.getId() == null) {
            return R.error("查询信息不能为空");
        }
        return userService.deactivateUser(user);
    }
}
